package com.xy.tianyu.module.splash

import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.lifecycle.rxLifeScope
import com.blankj.utilcode.util.*
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.tencent.mmkv.MMKV
import com.xy.tianyu.MainActivity
import com.xy.tianyu.R
import com.xy.tianyu.constant.Constant
import com.xy.tianyu.module.splash.bean.AdvertBean
import com.xy.tianyu.module.splash.bean.Vertical
import com.xy.tianyu.network.Api
import kotlinx.android.synthetic.main.activity_splash.*
import kotlinx.coroutines.delay
import rxhttp.RxHttp
import rxhttp.toWallpaperResponse
import java.text.SimpleDateFormat


class SplashActivity: AppCompatActivity() {

    private val mmkv:MMKV by lazy { MMKV.defaultMMKV() }

    private val splashDuration = 3 * 1000L

    private val scaleAnimation by lazy {
        ScaleAnimation(1f, 1.05f, 1f, 1.05f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f).apply {
            duration = splashDuration
            fillAfter = true
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        //BarUtils.setStatusBarColor(this, ContextCompat.getColor(this,android.R.color.transparent))
        val adverts = mmkv.decodeString(Constant.SPLASH_ADVERTS)
        val time = mmkv.decodeString(Constant.SPLASH_GET_TIME)
        LogUtils.e("时间",TimeUtils.getNowString(SimpleDateFormat("yyyyMMdd")))
        if (time.isNullOrEmpty() || time != TimeUtils.getNowString(SimpleDateFormat("yyyyMMdd"))){
            requestNet()
        }else{
            if (adverts.isNullOrEmpty()){
                requestNet()
            }else{
                val resultType = object : TypeToken<List<Vertical>>() {}.type
                val advertList = Gson().fromJson<List<Vertical>>(adverts,resultType)
                loadAdvert(advertList)
            }
        }
    }

    private fun hideSystemBar() {
        val decorView: View = window.decorView
        val uiOptions: Int = (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                or View.SYSTEM_UI_FLAG_FULLSCREEN
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
        decorView.systemUiVisibility = uiOptions
    }

    private fun requestNet(){
        rxLifeScope.launch ({
            val advertBean = getAdvert()
            advertBean?.let {
                val advertList = advertBean.vertical
                advertList?.let {
                    mmkv.encode(Constant.SPLASH_ADVERTS,GsonUtils.toJson(advertList))
                    mmkv.encode(Constant.SPLASH_GET_TIME,TimeUtils.getNowString(SimpleDateFormat("yyyyMMdd")))
                    loadAdvert(advertList)
                }
            }

        },{
            ActivityUtils.startActivity(MainActivity::class.java)
            finish()
            LogUtils.e(SplashActivity::class.simpleName,it.message)
        })
    }
    private fun loadAdvert(advertList:List<Vertical>){
        Glide.with(this@SplashActivity).asBitmap().load(advertList.random().img).into(object :SimpleTarget<Bitmap>(){
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                iv_advert.setImageBitmap(resource)
                iv_advert.startAnimation(scaleAnimation)
                rxLifeScope.launch {
                    delay(splashDuration)
                    ActivityUtils.startActivity(MainActivity::class.java)
                    finish()
                }
            }

        })
    }

    private suspend fun getAdvert():AdvertBean{
        return RxHttp.postJson(Api.WALLPAPER_BASE_URL+Api.WALLPAPER_CATEGORY_ID)
            .add("limit",30)
            .toWallpaperResponse<AdvertBean>()
            .await()
    }

}